package com.hxzy.dao.impl;

import com.hxzy.bean.User;
import com.hxzy.dao.UserDao;
import com.hxzy.util.JdbcUtils;

import java.util.List;

/**
 * @author nick
 */
public class UserDaoImpl implements UserDao {
    @Override
    public int save(User user) {
        String sql = "INSERT INTO message_users(nick_name,account,login_password,state) values (?,?,?,?)";
        return JdbcUtils.executeUpdate(sql,user.getNickName(),user.getAccount(),user.getPassword(),user.getState());
    }

    @Override
    public int update(User user) {
        String sql = "UPDATE message_users set nick_name=?,account=?,login_password=?,state=? where id=?";
        return JdbcUtils.executeUpdate(sql,user.getNickName(),user.getAccount(),user.getPassword(),user.getState(),user.getId());
    }

    @Override
    public int remove(Integer integer) {
        String sql = "DELETE FROM message_users where id=?";
        return JdbcUtils.executeUpdate(sql,integer);
    }

    @Override
    public List<User> queryAll() {
        String sql = new StringBuilder()
                .append("SELECT ")
                .append(" m.id as `id`,")
                .append(" m.nick_name as `nickName`,")
                .append(" m.account as `account`,")
                .append(" m.login_password as `password`,")
                .append(" m.state as `state`")
                .append(" FROM message_users m")
                .toString();
        System.out.println(sql);
        return JdbcUtils.selectSingleTab(User.class,sql);
    }

    @Override
    public User findById(Integer integer) {
        String sql = new StringBuilder()
                .append("SELECT ")
                .append(" m.id as `id`,")
                .append(" m.nick_name as `nickName`,")
                .append(" m.account as `account`,")
                .append(" m.login_password as `password`,")
                .append(" m.state as `state`")
                .append(" FROM message_users m")
                .append(" WHERE m.id=?")
                .toString();
        List<User> users = JdbcUtils.selectSingleTab(User.class, sql,integer);
        return users.size() > 0 ? users.get(0) : null;
    }

    @Override
    public User findByAccount(String account) {
        String sql = new StringBuilder()
                .append("SELECT ")
                .append(" m.id as `id`,")
                .append(" m.nick_name as `nickName`,")
                .append(" m.account as `account`,")
                .append(" m.login_password as `password`,")
                .append(" m.state as `state`")
                .append(" FROM message_users m")
                .append(" WHERE m.account=?")
                .toString();
        List<User> users = JdbcUtils.selectSingleTab(User.class, sql,account);
        return users.size() > 0 ? users.get(0) : null;
    }
}